Learning JavaScript: A Hands-On Guide to the Fundamentals of Modern JavaScript (Shawn Kahl's Library) by Tim Wright
Author:Tim Wright
Language: eng
Format: epub
Publisher: Addison-Wesley Professional
Published: 2013-06-30T16:00:00+00:00
Returning the Data
Right now the data is stuck inside the onreadystatechange event handler. This obviously isn’t what we want. If we were using this function only once, we could add all the data parsing and output right inside the event handler. You could build out all your functionality right there inside the ajaxCall() function. That would be perfectly fine—a little difficult to integrate into our current codebase, but there’s nothing wrong with that.
Lucky for you, we’re not going to settle for “just fine.” We can make this function a lot more reusable by finding a way to work with the data it’s returning while still remaining inside the context of the onreadystatechange event handler. To accomplish this goal, we need to find a way to call the ajaxCall() function and allow it to take an extra argument that is a function. This does get a little tricky, but a function that calls another function isn’t a foreign concept at this point. Remember? A function that calls another function is called a callback function. With that in mind, what we need to do is allow the ajaxCall() function to accept a callback function.
The first step to doing this is to save the responseText to a variable so we can work with it more easily. Listing 8.5.1 introduces a new argument called callback, which will represent the function we will be passing through to access the data response. So we will have two arguments; one will be a string, and the other will be a function.
Because the data is saved to a variable (contacts), it is easy to pass that data into the callback function.
Listing 8.5.1. Accepting a Callback Function
Click here to view code image
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
What's Done in Darkness by Kayla Perrin(26593)
The Fifty Shades Trilogy & Grey by E L James(19079)
Shot Through the Heart: DI Grace Fisher 2 by Isabelle Grey(19058)
Shot Through the Heart by Mercy Celeste(18936)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 10 by Isuna Hasekura and Jyuu Ayakura(17112)
Python GUI Applications using PyQt5 : The hands-on guide to build apps with Python by Verdugo Leire(16994)
Peren F. Statistics for Business and Economics...Essential Formulas 3ed 2025 by Unknown(16878)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 03 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16825)
Wolf & Parchment: New Theory Spice & Wolf, Vol. 01 by Isuna Hasekura and Jyuu Ayakura & Jyuu Ayakura(16447)
The Subtle Art of Not Giving a F*ck by Mark Manson(14353)
The 3rd Cycle of the Betrayed Series Collection: Extremely Controversial Historical Thrillers (Betrayed Series Boxed set) by McCray Carolyn(14141)
Stepbrother Stories 2 - 21 Taboo Story Collection (Brother Sister Stepbrother Stepsister Taboo Pseudo Incest Family Virgin Creampie Pregnant Forced Pregnancy Breeding) by Roxi Harding(13646)
Scorched Earth by Nick Kyme(12771)
Drei Generationen auf dem Jakobsweg by Stein Pia(10965)
Suna by Ziefle Pia(10889)
Scythe by Neal Shusterman(10338)
The Ultimate Python Exercise Book: 700 Practical Exercises for Beginners with Quiz Questions by Copy(9562)
D:\Jan\FTP\HOL\Work\Alien Breed - Tower Assault CD32 Alien Breed II - The Horror Continues Manual 1.jpg by PDFCreator(9535)
International Relations from the Global South; Worlds of Difference; First Edition by Arlene B. Tickner & Karen Smith(9522)
